﻿/// <reference name="MicrosoftAjax.js"/>
/// <author name="Argo Zhang" msn="argo@live.ca" />

Type.registerNamespace("Longbow.Web.UI");

Longbow.Web.UI.Watermark = function(element) {
    Longbow.Web.UI.Watermark.initializeBase(this, [element]);
    this._watermarkText = "";
    this._watermarkCss = "";
    this._blurHandler = null;
    this._focusHandler = null;
    this._keyPressHandler = null;
    this._edit = false;
}

Longbow.Web.UI.Watermark.prototype = {
    initialize: function() {
        Longbow.Web.UI.Watermark.callBaseMethod(this, 'initialize');
        
        // Add custom initialization here
        var element = this.get_element();        
        this._blurHandler = Function.createDelegate(this, this._onBlur);
        $addHandler(element, "blur", this._blurHandler);
        this._focusHandler = Function.createDelegate(this, this._onFocus);
        $addHandler(element, "focus", this._focusHandler);
        this._keyPressHandler = Function.createDelegate(this, this._onKeyPress);
        $addHandler(element, "keypress", this._keyPressHandler);
        
        this._edit = element.value.length > 0;
        if(this._edit) 
            element.select();
        else {
            element.focus();
            element.blur();
        }
    },
    
    _onKeyPress: function(eventObject) {
        var element = this.get_element();
        this._edit = true;
        Sys.UI.DomElement.removeCssClass(element, this.get_watermarkCss());
    },
    
    _onBlur: function(eventObject) {
        var element = this.get_element();
        this._edit = element.value.length > 0;
        if (!this._edit) {
            element.value = this.get_watermarkText();
            Sys.UI.DomElement.addCssClass(element, this.get_watermarkCss());
        }
    },
    
    _onFocus: function(eventObject) {
        var element = this.get_element();
        if (!this._edit) {
            element.value = "";
            Sys.UI.DomElement.removeCssClass(element, this.get_watermarkCss());            
        }
    },
    
    get_watermarkText: function() {
        /// <summary></summary>
        if (arguments.length !== 0) throw Error.parameterCount();         
        return this._watermarkText;
    },
    
    set_watermarkText: function(value) {
        /// <param name="value" type="String" />
        var e = Function._validateParams(arguments, [
            {name: "value", type: String}
        ]);
        if (e) throw e;
        
        this._watermarkText = value;
    },
    
    get_text: function() {
        var element = this.get_element();
        return this._edit ? element.value : "";
    },
    
    get_watermarkCss: function() {
        /// <summary></summary>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._watermarkCss;
    },
    
    set_watermarkCss: function(value) {
        /// <param name="value" type="String" />
        var e = Function._validateParams(arguments, [
            {name: "value", type: String}
        ]);
        if (e) throw e;
        
        this._watermarkCss = value;
    },
    
    dispose: function() {        
        //Add custom dispose actions here
        if (this._blurHandler !== null) {
            $removeHandler(this.get_element(), "blur", this._blurHandler);
            this._blurHandler = null;
        }
        if (this._focusHandler !== null) {
            $removeHandler(this.get_element(), "focus", this._focusHandler);
            this._focusHandler = null;
        }
        if (this._keyPressHandler !== null) {
            $removeHandler(this.get_element(), "keypress", this._keyPressHandler);
            this._keyPressHandler = null;
        }

        Longbow.Web.UI.Watermark.callBaseMethod(this, 'dispose');
    }
}
Longbow.Web.UI.Watermark.registerClass('Longbow.Web.UI.Watermark', Sys.UI.Behavior);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
